
package com.trp.view;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.Context;

import com.trp.model.CIMModel;
import com.trp.model.Substation;
import com.trp.model.asset.Pole;
import com.trp.model.psr.Cable;
import com.trp.model.psr.OverheadCircuit;

public class Builder {

    /**
     * Build view objects According to CIM model data. TODO should use static
     * factory pattern instead of current solution.
     *
     * @param context
     * @param l
     * @param m
     */
    public void buildView(Context context, List<GISView> l, Map<String, CIMModel> m) {
        Iterator<CIMModel> itr = m.values().iterator();
        GISView gView = null;
        while (itr.hasNext()) {
            CIMModel model = itr.next();
            CIMModel.Type cType = model.getType();
            if (cType == null) {
                continue;
            }
            switch (cType) {
                case POLE:
                    gView = buildPole(context, (Pole)model);
                    break;
                case SUBSTATION:
                    gView = buildSubstation(context, (Substation)model);
                    break;
                case CABLE:
                    gView = buildCable(context, (Cable)model);
                    break;
                case OVERHEAD_CIRCUIT:
                    gView = buildOverheadCircuit(context, (OverheadCircuit)model);
                    break;
            }
            if (gView != null) {
                l.add(gView);
            }

        }
    }

    private GISView buildPole(Context context, Pole p) {
        PoleView pv = new PoleView(context, p.getX(), p.getY());
        return pv;
    }

    private GISView buildOverheadCircuit(Context context, OverheadCircuit p) {
        float[] dAr = p.getPoints();
        float[] fAr = new float[dAr.length];
        System.arraycopy(dAr, 0, fAr, 0, dAr.length);
        SegmentView sv = new SegmentView(context, fAr);
        return sv;
    }

    private GISView buildCable(Context context, Cable c) {
        double[] dAr = c.getPoints();
        float[] fAr = new float[dAr.length];
        System.arraycopy(dAr, 0, fAr, 0, dAr.length);
        CableView cv = new CableView(context, fAr);
        return cv;
    }

    private GISView buildSubstation(Context context, Substation subst) {
        SubstationView sv = new SubstationView(context, subst.getX(), subst.getY());
        return sv;
    }

}
